package com.frinder.hadoop.hbase.base;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.List;

/**
 * @author frinder
 * @date 2018/7/3
 * @Description: ${todo}
 */
@Slf4j
public class ResultUtils {

    /**
     * @param result
     * @param toClass
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> T convertToBean(Result result, Class<T> toClass) throws Exception {
        log.info("Result: {}", result);
        T entity = toClass.newInstance();
        BeanWrapper wrapper = new BeanWrapperImpl(entity);
        //
        List<Cell> cellList = result.listCells();
        cellList.stream()
                .forEach(cell ->
                        wrapper.setPropertyValue(
                                new String(CellUtil.cloneQualifier(cell)),
                                new String(CellUtil.cloneValue(cell))
                        )
                );
        return entity;
    }

}
